<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<input type="text" id="txt">
<button id="send">发送</button>
</body>
<script>
    window.onload = function () {
        // 实例化一个websocket对象，注意是ws协议，所以是ws://开头的url地址
        var ws = new WebSocket("ws://localhost:8001")
        ws.onopen = () => {
            console.log('已经连接到')
            send.onclick = function() {
                ws.send(txt.value)
            }
        }
        ws.onmessage = txt => {
            console.log(txt)
        }
    }
</script>

</html>